Micron Document
🎖️GitЯра🎖️

Node / meshtastic / Meshtastic-Android / files / core / ui / src / commonMain / kotlin / org / meshtastic / core / ui / util / ProtoExtensions.kt

Displaying Raw • Download

core/ui/src/commonMain/kotlin/org/meshtastic/core/ui/util/ProtoExtensions.kt 2dba958e1e633f2ad76913f726694eb2ec9d65ec (2dba958e) Text, 7.77 KB

T8b949e/*
* Copyright (c) 2026 Meshtastic LLC
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
Tff7b72package T7ee787org.meshtastic.core.ui.util

Tff7b72import T7ee787androidx.compose.runtime.Composable
Tff7b72import T7ee787kotlinx.coroutines.flow.first
Tff7b72import T7ee787org.jetbrains.compose.resources.stringResource
Tff7b72import T7ee787org.meshtastic.core.common.util.DateFormatter
Tff7b72import T7ee787org.meshtastic.core.common.util.nowMillis
Tff7b72import T7ee787org.meshtastic.core.repository.RadioConfigRepository
Tff7b72import T7ee787org.meshtastic.core.repository.RadioController
Tff7b72import T7ee787org.meshtastic.core.resources.Res
Tff7b72import T7ee787org.meshtastic.core.resources.unknown_age
Tff7b72import T7ee787org.meshtastic.proto.Channel
Tff7b72import T7ee787org.meshtastic.proto.ChannelSet
Tff7b72import T7ee787org.meshtastic.proto.ChannelSettings
Tff7b72import T7ee787org.meshtastic.proto.MeshPacket
Tff7b72import T7ee787org.meshtastic.proto.Position
Tff7b72import T7ee787kotlin.time.Duration.Companion.days

Tff7b72private Tff7b72const Tff7b72val Te6edf3SECONDS_TO_MILLIS Tff7b72= T79c0ff1T79c0ff0T79c0ff0T79c0ff0L

Tf0883e@Composable
Tff7b72fun Te6edf3PositionTb4b4b4.Td2a8ffformatPositionTimeTb4b4b4(Tb4b4b4)Tb4b4b4: Tffa657String Tb4b4b4{
Tff7b72val Te6edf3currentTime Tff7b72= Te6edf3nowMillis
Tff7b72val Te6edf3sixMonthsAgo Tff7b72= Te6edf3currentTime Tff7b72- T79c0ff1T79c0ff8T79c0ff0.Te6edf3daysTb4b4b4.Te6edf3inWholeMilliseconds
Tff7b72val Te6edf3isOlderThanSixMonths Tff7b72= Te6edf3time Tff7b72* Te6edf3SECONDS_TO_MILLIS Tff7b72< Te6edf3sixMonthsAgo
Tff7b72val Te6edf3timeText Tff7b72=
Tff7b72if Tb4b4b4(Te6edf3isOlderThanSixMonthsTb4b4b4) Tb4b4b4{
Te6edf3stringResourceTb4b4b4(Te6edf3ResTb4b4b4.Te6edf3stringTb4b4b4.Te6edf3unknown_ageTb4b4b4)
Tb4b4b4} Tff7b72else Tb4b4b4{
Te6edf3DateFormatterTb4b4b4.Te6edf3formatDateTimeTb4b4b4(Te6edf3time Tff7b72* Te6edf3SECONDS_TO_MILLISTb4b4b4)
Tb4b4b4}
Tff7b72return Te6edf3timeText
Tb4b4b4}

Tff7b72fun Te6edf3MeshPacketTb4b4b4.Td2a8fftoPositionTb4b4b4(Tb4b4b4)Tb4b4b4: Te6edf3Position? Tb4b4b4{
Tff7b72val Te6edf3decoded Tff7b72= Te6edf3decoded Tff7b72?: Tff7b72return Tff7b72null
Tff7b72return Tff7b72if Tb4b4b4(Te6edf3decodedTb4b4b4.Te6edf3want_response Tff7b72!Tff7b72= Tff7b72trueTb4b4b4) Tb4b4b4{
Te6edf3decodedTb4b4b4.Te6edf3payloadTb4b4b4.Te6edf3let Tb4b4b4{ Te6edf3runCatching Tb4b4b4{ Te6edf3PositionTb4b4b4.Te6edf3ADAPTERTb4b4b4.Te6edf3decodeTb4b4b4(Tffa657itTb4b4b4) Tb4b4b4}Tb4b4b4.Te6edf3getOrNullTb4b4b4(Tb4b4b4) Tb4b4b4}
Tb4b4b4} Tff7b72else Tb4b4b4{
Tff7b72null
Tb4b4b4}
Tb4b4b4}

T8b949e/**
* Builds a [Channel] list from the difference between two [ChannelSettings] lists. Only changes are included in the
* resulting list.
*
* @param new The updated [ChannelSettings] list.
* @param old The current [ChannelSettings] list (required when disabling unused channels).
* @return A [Channel] list containing only the modified channels.
*/
Tff7b72fun Td2a8ffgetChannelListTb4b4b4(Te6edf3newTb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelSettingsTff7b72>Tb4b4b4, Te6edf3oldTb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelSettingsTff7b72>Tb4b4b4)Tb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelTff7b72> Tff7b72= Te6edf3buildList Tb4b4b4{
Tff7b72for Tb4b4b4(Te6edf3i Tff7b72in T79c0ff0.Tb4b4b4.Te6edf3maxOfTb4b4b4(Te6edf3oldTb4b4b4.Te6edf3lastIndexTb4b4b4, Te6edf3newTb4b4b4.Te6edf3lastIndexTb4b4b4)Tb4b4b4) Tb4b4b4{
Tff7b72if Tb4b4b4(Te6edf3oldTb4b4b4.Te6edf3getOrNullTb4b4b4(Te6edf3iTb4b4b4) Tff7b72!Tff7b72= Te6edf3newTb4b4b4.Te6edf3getOrNullTb4b4b4(Te6edf3iTb4b4b4)Tb4b4b4) Tb4b4b4{
Te6edf3addTb4b4b4(
Te6edf3ChannelTb4b4b4(
Te6edf3role Tff7b72=
Tff7b72when Tb4b4b4(Te6edf3iTb4b4b4) Tb4b4b4{
T79c0ff0 Tff7b72-Tff7b72> Te6edf3ChannelTb4b4b4.Te6edf3RoleTb4b4b4.Te6edf3PRIMARY
Tff7b72in T79c0ff1.Tb4b4b4.Te6edf3newTb4b4b4.Te6edf3lastIndex Tff7b72-Tff7b72> Te6edf3ChannelTb4b4b4.Te6edf3RoleTb4b4b4.Te6edf3SECONDARY
Tff7b72else Tff7b72-Tff7b72> Te6edf3ChannelTb4b4b4.Te6edf3RoleTb4b4b4.Te6edf3DISABLED
Tb4b4b4}Tb4b4b4,
Te6edf3index Tff7b72= Te6edf3iTb4b4b4,
Te6edf3settings Tff7b72= Te6edf3newTb4b4b4.Te6edf3getOrNullTb4b4b4(Te6edf3iTb4b4b4) Tff7b72?: Te6edf3ChannelSettingsTb4b4b4(Tb4b4b4)Tb4b4b4,
Tb4b4b4)Tb4b4b4,
Tb4b4b4)
Tb4b4b4}
Tb4b4b4}
Tb4b4b4}

T8b949e/**
* Builds an authoritative [Channel] list for a full REPLACE import. Every position in [new] is emitted (PRIMARY at
* index 0, SECONDARY for 1..new.lastIndex) and any trailing positions beyond [new]'s range are emitted as DISABLED so
* the radio stops using them.
*
* Unlike [getChannelList], this does NOT skip positions where `currentSettings[i] == new[i]`: the imported set is
* authoritative, the local cache must not gate the writes, and silent diff-skips during REPLACE were the source of
* stale channels.
*
* [currentSettings] is consulted only for its size (to determine trailing DISABLED writes); its values are never
* compared against [new]. Callers should read it from `radioConfigRepository.channelSetFlow.first().settings`, not from
* a `stateInWhileSubscribed` StateFlow's `.value` — the StateFlow placeholder window can return an empty list and
* suppress trailing DISABLED writes.
*
* Edge case: if [new] is empty, every emitted slot (including index 0) is DISABLED rather than wrongly promoting an
* empty [ChannelSettings] to PRIMARY.
*
* @param new The imported [ChannelSettings] list. Every index becomes a write to the radio.
* @param currentSettings The current [ChannelSettings] list. Only its size is used; trailing indices past [new] become
* DISABLED writes so leftover slots are cleared.
* @return A [Channel] list covering every slot the radio needs written to materialize [new] and clear leftover slots.
*/
Tff7b72fun Td2a8ffgetChannelReplacementListTb4b4b4(Te6edf3newTb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelSettingsTff7b72>Tb4b4b4, Te6edf3currentSettingsTb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelSettingsTff7b72>Tb4b4b4)Tb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelTff7b72> Tff7b72=
Te6edf3buildList Tb4b4b4{
Tff7b72for Tb4b4b4(Te6edf3i Tff7b72in T79c0ff0.Tb4b4b4.Te6edf3maxOfTb4b4b4(Te6edf3currentSettingsTb4b4b4.Te6edf3lastIndexTb4b4b4, Te6edf3newTb4b4b4.Te6edf3lastIndexTb4b4b4)Tb4b4b4) Tb4b4b4{
Te6edf3addTb4b4b4(
Te6edf3ChannelTb4b4b4(
Te6edf3role Tff7b72=
Tff7b72when Tb4b4b4(Te6edf3iTb4b4b4) Tb4b4b4{
T8b949e// Empty-new is a degenerate import: every slot (including 0) must be DISABLED.
T79c0ff0 Tff7b72-Tff7b72> Tff7b72if Tb4b4b4(Te6edf3newTb4b4b4.Te6edf3isEmptyTb4b4b4(Tb4b4b4)Tb4b4b4) Te6edf3ChannelTb4b4b4.Te6edf3RoleTb4b4b4.Te6edf3DISABLED Tff7b72else Te6edf3ChannelTb4b4b4.Te6edf3RoleTb4b4b4.Te6edf3PRIMARY

Tff7b72in T79c0ff1.Tb4b4b4.Te6edf3newTb4b4b4.Te6edf3lastIndex Tff7b72-Tff7b72> Te6edf3ChannelTb4b4b4.Te6edf3RoleTb4b4b4.Te6edf3SECONDARY

Tff7b72else Tff7b72-Tff7b72> Te6edf3ChannelTb4b4b4.Te6edf3RoleTb4b4b4.Te6edf3DISABLED
Tb4b4b4}Tb4b4b4,
Te6edf3index Tff7b72= Te6edf3iTb4b4b4,
Te6edf3settings Tff7b72= Te6edf3newTb4b4b4.Te6edf3getOrNullTb4b4b4(Te6edf3iTb4b4b4) Tff7b72?: Te6edf3ChannelSettingsTb4b4b4(Tb4b4b4)Tb4b4b4,
Tb4b4b4)Tb4b4b4,
Tb4b4b4)
Tb4b4b4}
Tb4b4b4}

T8b949e/**
* Applies an imported [ChannelSet] as an authoritative replacement to the radio and local cache.
*
* Reads the current channel set from [radioConfigRepository]'s flow (avoiding the StateFlow placeholder window), builds
* the authoritative replacement list via [getChannelReplacementList], enqueues each channel write to the radio
* sequentially via [radioController], then atomically replaces the local cached settings.
*
* setLocalChannel returns once the packet is enqueued, not after firmware ACK — firmware echoes via
* MeshConfigHandlerImpl can still arrive after [radioConfigRepository.replaceAllSettings] and are tracked separately.
*
* Does NOT handle LoRa config — callers are responsible for comparing and sending `lora_config` if present.
*
* @param channelSet The imported [ChannelSet] to apply as a replacement.
* @param radioController The [RadioController] used to enqueue channel writes.
* @param radioConfigRepository The [RadioConfigRepository] providing the current channel flow and cache.
*/
Tff7b72suspend Tff7b72fun Td2a8ffapplyReplacementChannelSetTb4b4b4(
Te6edf3channelSetTb4b4b4: Te6edf3ChannelSetTb4b4b4,
Te6edf3radioControllerTb4b4b4: Te6edf3RadioControllerTb4b4b4,
Te6edf3radioConfigRepositoryTb4b4b4: Te6edf3RadioConfigRepositoryTb4b4b4,
Tb4b4b4) Tb4b4b4{
Tff7b72val Te6edf3currentSettings Tff7b72= Te6edf3radioConfigRepositoryTb4b4b4.Te6edf3channelSetFlowTb4b4b4.Te6edf3firstTb4b4b4(Tb4b4b4)Tb4b4b4.Te6edf3settings
Tff7b72for Tb4b4b4(Te6edf3channel Tff7b72in Te6edf3getChannelReplacementListTb4b4b4(Te6edf3channelSetTb4b4b4.Te6edf3settingsTb4b4b4, Te6edf3currentSettingsTb4b4b4)Tb4b4b4) Tb4b4b4{
Te6edf3radioControllerTb4b4b4.Te6edf3setLocalChannelTb4b4b4(Te6edf3channelTb4b4b4)
Tb4b4b4}
Te6edf3radioConfigRepositoryTb4b4b4.Te6edf3replaceAllSettingsTb4b4b4(Te6edf3channelSetTb4b4b4.Te6edf3settingsTb4b4b4)
Tb4b4b4}

T8b949e/**
* Builds the ADD-mode preview list for QR import. Existing channels are preserved at their positions; every incoming
* channel is appended in order without deduplication.
*
* Structural `.distinct()` was previously used here, but it silently dropped incoming channels that matched existing
* entries, shifting later channels to wrong indices and hiding them from the user. The caller (UI) lets the user select
* which incoming channels to keep.
*
* @param existing The current [ChannelSettings] list on the radio. Preserved in order.
* @param incoming The imported [ChannelSettings] list. Appended in order.
* @return The concatenated list `[existing..., incoming...]`.
*/
Tff7b72fun Td2a8ffmergeChannelSettingsForAddTb4b4b4(
Te6edf3existingTb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelSettingsTff7b72>Tb4b4b4,
Te6edf3incomingTb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelSettingsTff7b72>Tb4b4b4,
Tb4b4b4)Tb4b4b4: Te6edf3ListTff7b72<Te6edf3ChannelSettingsTff7b72> Tff7b72= Te6edf3existing Tff7b72+ Te6edf3incoming

Served by rngit 1.5.2 - Generated in 0.1s